package com.lee.crawler.gov.strategy.list;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.lee.crawler.gov.strategy.FietArticleStrategy;

@Component
public class FietListStrategy implements ListStrategy {

	private static final String fietNextPage = "http://www.fiet.gov.cn/xxgk/tzgg/index_%d.htm";
	private static final Pattern pattern = Pattern.compile("pageCount:(\\d+)");

	@Autowired
	private FietArticleStrategy fietArticleStrategy;

	public static void main(String[] args) throws UnsupportedEncodingException {

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml",
				"spring/applicationContext-datasource.xml", "spring/applicationContext-redis.xml");
		context.start();
		Arrays.asList(context.getBeanDefinitionNames()).forEach(System.out::println);

		FietListStrategy bean = (FietListStrategy) context.getBean("fietListStrategy");
		bean.saveCrawlerArticles();

		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (context != null) {
				context.close();
			}
		}

	}

	@Override
	public String setCategroyUrl() {
		return "http://www.fiet.gov.cn/xxgk/tzgg/index.htm";
	}

	@Override
	public void saveCrawlerArticles() {
		savePage(setCategroyUrl());
		Document contentStr = getContent(setCategroyUrl());
		Matcher matcher = pattern.matcher(contentStr.toString());
		if (matcher.find()) {
			Integer PageTotal = Integer.valueOf(matcher.group(1));
			for (int i = 1; i < PageTotal; i++) {
				savePage(String.format(fietNextPage, i));
			}
		}

	}

	public void savePage(String pageUrl) {

		Document contentStr = getContent(pageUrl);
		List<String> links = contentStr.select("div.gl_box li a").eachAttr("abs:href");
		links.stream().forEach(link -> {
			try {
				fietArticleStrategy.saveCrawlerArticle(link);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

	}

}
